package com.shop.service.impl;

import com.shop.common.SetMeta;
import com.shop.entity.reports.*;
import com.shop.exception.ReportsException;
import com.shop.mapper.ReportsMapper;
import com.shop.service.ReportsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("reportsService")
public class ReportsServiceImpl implements ReportsService {

    @Autowired
    private ReportsMapper reportsMapper;

    @Override
    public SetMeta reportsOne() throws ReportsException {
        SetMeta setMeta = new SetMeta();

        List<ReportsSql> reportsSqls = reportsMapper.selectList(null);
        if (reportsSqls == null) {
            throw new ReportsException("查询的列表为空");
        }
        List<String> names = new ArrayList<>();
        List<Date> dates = new ArrayList<>();
        List<Series> series = new ArrayList<>();
        for (ReportsSql reportsSql : reportsSqls) {
            if (!names.contains(reportsSql.getRp1_area())) {
                names.add(reportsSql.getRp1_area());
            }
            if (!dates.contains(reportsSql.getRp1_date())) {
                dates.add(reportsSql.getRp1_date());
            }

            String rpArea = reportsSql.getRp1_area();
            boolean flag = true;
            for (Series series1 : series) {
                if (series1.getName().equals(rpArea)) {
                    series1.getData().add(reportsSql.getRp1_user_count());
                    flag = false;
                }
            }
            if (flag) {
                Series series1 = new Series(rpArea, "line", "总量", new AreaStyle(), new ArrayList<>());
                series1.getData().add(reportsSql.getRp1_user_count());
                series.add(series1);
            }
        }

        ReportsOne reportsOne = new ReportsOne();
        reportsOne.setLegend(new Common<>());
        reportsOne.getLegend().setData(names);

        reportsOne.setYAxis(new ArrayList<>());
        reportsOne.getYAxis().add(new CommonType());

        reportsOne.setXAxis(new ArrayList<>());
        Common<Date> common = new Common<>();
        common.setData(dates);
        reportsOne.getXAxis().add(common);

        reportsOne.setSeries(series);

        setMeta.setSuccess(reportsOne,"获取报表成功");
        return setMeta;
    }
}
